package com.bms.service.impl;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import com.bms.dao.VisitStatisDao;
import com.bms.entity.VisitStatis;
import com.bms.httpModel.StatisPager;
import com.bms.service.VisitStatisService;
import com.bms.utils.DateUtil;

@Service
public class VisitStatisServiceImpl extends BaseServiceImpl<VisitStatis, String> implements VisitStatisService {

	@Resource
	private VisitStatisDao visitStatisDao;

	@Resource
	public void setBaseDao(VisitStatisDao visitStatisDao) {
		super.setBaseDao(visitStatisDao);
	}

	/**
	 * @return the visitStatisDao
	 */
	public VisitStatisDao getVisitStatisDao() {
		return visitStatisDao;
	}

	/**
	 * @param visitStatisDao the visitStatisDao to set
	 */
	public void setVisitStatisDao(VisitStatisDao visitStatisDao) {
		this.visitStatisDao = visitStatisDao;
	}
	
	
	public StatisPager findByPager(VisitStatis bean, StatisPager pager) {
		DetachedCriteria criteria = DetachedCriteria.forClass(VisitStatis.class);
		if(bean != null){
			//拼接where
			if(StringUtils.isNotBlank(bean.getBeginDate())){
				criteria.add(Restrictions.ge("statisDate", DateUtil.parseDate(bean.getBeginDate())));
			}
			if(StringUtils.isNotBlank(bean.getEndDate())){
				criteria.add(Restrictions.le("statisDate", DateUtil.getEndOfDate(bean.getEndDate())));
			}
			if(StringUtils.isNotBlank(bean.getAreaName())){//根据选择的区域过滤统计
				criteria.add(Restrictions.eq("areaName", bean.getAreaName()));
			}
			if(StringUtils.isNotBlank(bean.getLink())){//根据选择的应用过滤统计
				criteria.add(Restrictions.like("link", bean.getLink(), MatchMode.ANYWHERE));
			}
		}
		pager.setDetachedCriteria(criteria);
		return visitStatisDao.findByPager(pager);
	}

}